package P912;

import org.junit.Test;

/**
 * @Author DJ同学
 * @Date 2021/2/28 8:21
 * @Version 1.0
 * @Name 排序数组
 * @Problem https://leetcode-cn.com/problems/sort-an-array/
 * 插入排序
 *
 */
public class Solution3 {
    @Test
    public void test(){
        for (int i : sortArray(new int[]{5, 2, 3, 1})) {
            System.out.println(i);
        }
        return;
    }
    public int[] sortArray(int[] nums) {
        //将当前数字提取出来，和前面排序好的数组对比，大的数需要不断的向后移动
        int curNum;
        int preIndex;
        for(int i = 1;i<nums.length;i++){
            preIndex = i-1;
            curNum = nums[i];
            while(preIndex>=0&&nums[preIndex]>curNum){
                nums[preIndex+1] = nums[preIndex];
                preIndex--;
            }
            nums[preIndex+1] = curNum;
        }
        return nums;
    }
}
